package io.github.topcatv.mall.common.ao;

import com.google.common.collect.Maps;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;
import java.util.Optional;

/**
 * @author liuyi
 */
@Data
public class ReturnObject implements Serializable {
    private Integer code;
    private String message;
    private Map data;

    private ReturnObject(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.data = Maps.newHashMap();
    }

    private ReturnObject(Integer code, Map data) {
        this.code = code;
        this.message = "";
        Optional<Map> optional = Optional.ofNullable(data);
        this.data = optional.orElse(Maps.newHashMap());
    }

    public static ReturnObject error(String message) {
        return new ReturnObject(500, message);
    }

    public static ReturnObject forbidden(){
        return new ReturnObject(403, "没有权限");
    }

    public static ReturnObject unauthorized(){
        return new ReturnObject(401, "没有登录");
    }

    public static ReturnObject success() {
        return new ReturnObject(200, Maps.newHashMap());
    }

    public static ReturnObject success(Map data) {
        return new ReturnObject(200, data);
    }

    public ReturnObject put(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public ReturnObject putAll(Map data) {
        this.data.putAll(data);
        return this;
    }
}
